package com.sec.samsung.android.app.baymax.view.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.sec.samsung.android.app.baymax.R;
import com.sec.samsung.android.app.baymax.utils.BaymaxConstant;
import com.sec.samsung.android.app.baymax.utils.SharePrefUtils;

public class TermsOfUseActivity extends Activity implements OnClickListener, OnCheckedChangeListener {

    private static final int REQUEST_CODE_PRIVACY = 200;

    private boolean mTermsAgreed = false;

    private LinearLayout mLayoutNext = null;
    private ProgressDialog mProgressDialog = null;

    private CheckBox mCheckTerms = null;
    private ScrollView mScrollTerms = null;

    private ImageView mDivider = null;
    private TextView mButtonNext = null;
    private String mCountryCode = "CN";
    private String mTermsOfCondition = null;

    @SuppressLint("DefaultLocale")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_terms_of_use);

        updateLayout();

    }

    private void updateLayout() {
        if (mTermsOfCondition == null) {
            mTermsOfCondition = getString(R.string.terms_of_use_disclaimer_description);
        }

        TextView textTerms = (TextView) findViewById(R.id.text_terms);
        textTerms.setText(mTermsOfCondition);
        mLayoutNext = (LinearLayout) findViewById(R.id.layout_next);
        mCheckTerms = (CheckBox) findViewById(R.id.checkbox_policy);
        mScrollTerms = (ScrollView) findViewById(R.id.scroll_terms);
        mDivider = (ImageView) findViewById(R.id.image_divider);
        mButtonNext = (TextView) findViewById(R.id.button_next);
        mCheckTerms.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        mButtonNext.setEnabled(false);
        mLayoutNext.setOnClickListener(this);

        if (mTermsAgreed == true) {
            mLayoutNext.setEnabled(true);
            mButtonNext.setEnabled(true);
        } else {
            mLayoutNext.setEnabled(false);
            mButtonNext.setEnabled(false);
        }

        mCheckTerms.setOnCheckedChangeListener(this);
        mCheckTerms.setVisibility(View.VISIBLE);

        mScrollTerms.setVisibility(View.VISIBLE);
        if (mDivider != null)
            mDivider.setVisibility(View.VISIBLE);

        mCheckTerms.setChecked(mTermsAgreed);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mProgressDialog != null) {
                            mProgressDialog.dismiss();
                            mProgressDialog = null;
                        }
                    }
                });
            }
        }, 200);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(TermsOfUseActivity.this, ProfileActivity.class);
        SharePrefUtils.putBooleanValue(this, BaymaxConstant.KEY_PASSED_FIRST_TIME_INITIALIZE, true);
        intent.putExtra("countryCode", mCountryCode);
        startActivityForResult(intent, REQUEST_CODE_PRIVACY);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton button, boolean enabled) {
        switch (button.getId()) {
        case R.id.checkbox_policy:
            mTermsAgreed = enabled;
            break;
        }

        if (mTermsAgreed == true) {
            mLayoutNext.setEnabled(true);
            mButtonNext.setEnabled(true);
        } else {
            mLayoutNext.setEnabled(false);
            mButtonNext.setEnabled(false);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_CODE_PRIVACY) {
            if (resultCode == RESULT_OK) {
                setResult(RESULT_OK);
                finish();
            }
        }
    }
}
